package image;

import java.awt.event.*;
import javax.swing.*;

/**A button which fires repeating events if held down.*/
public class RepeatButton extends JButton {
	private static final long serialVersionUID = -3938261395033297265L;
	private boolean adjusting;

    public RepeatButton(Icon icon) {
        super(icon);
        adjusting=false;
        setFocusPainted(false);
        addMouseListener(new RepeatListener(this));
    }
    
    public boolean getValueIsAdjusting()
    	{
    	return adjusting;
    	}

    private class RepeatListener extends MouseAdapter {

        RepeatThread repeatThread;
        RepeatButton parent;
        
        public RepeatListener(RepeatButton p)
        	{
        	super();
        	parent=p;
        	}
        
        public void mousePressed(MouseEvent e) {
            repeatThread = new RepeatThread(parent);
            repeatThread.start();
        }

        public void mouseReleased(MouseEvent e) {
            repeatThread.kill = true;
            repeatThread = null;
        }

    }

    private class RepeatThread extends Thread {

        boolean kill = false;
        RepeatButton parent;
        
        public RepeatThread(RepeatButton p)
        	{
        	super();
        	parent=p;
        	}

        public void run() {
            try {
                Thread.sleep(200);
            } catch(InterruptedException e) {}
            while(!kill) {
            	adjusting=true;
                doClick();
                try {
                    Thread.sleep(33);
                } catch(InterruptedException e) {}
            }
            if(adjusting) {
            	adjusting=false;
            	fireActionPerformed(new ActionEvent(parent, ActionEvent.ACTION_LAST, "LAST"));
            }
        }

    }

}